<?php

class Contacto{
	var $db;
	var $cont_cod;	
	var $usu_id;
	var $cont_nombre;
	var $cont_telefono;
	var $cont_celular;
	var $cont_email;

	
	/***********************************************
	*constructor
	***********************************************/
	function __construct($db,$cont_cod=NULL,$usu_id=NULL,$cont_nombre=NULL,$cont_telefono=NULL,$cont_celular=NULL,$cont_email=NULL){
		$this->db=$db;
		$this->cont_cod=$cont_cod;
		$this->usu_id=$usu_id;
		$this->cont_nombre=$cont_nombre;
		$this->cont_telefono=$cont_telefono;
		$this->cont_celular=$cont_celular;
		$this->cont_email=$cont_email;
	}
	
	/***********************************************
	* funcion getDatos
	* setea los datos de acuerdo al id
	***********************************************/
	function getDatos(){
		if($this->cont_cod!=NULL){
			$sql = 	"SELECT c.`usu_id`, c.`cont_nombre`, c.`cont_telefono`, c.`cont_celular`, c.`cont_email`
					FROM contactos c
					WHERE c.cont_cod='".$this->cont_cod."'";
			//die($sql." ");		
			$result = $this->db->query($sql);
			if ($result === false) die("failed getDatos Contactos");
			$result = $result->fetch_assoc();
			
			$this->usu_id=$result['usu_id'];
			$this->cont_nombre=$result['cont_nombre'];
			$this->cont_telefono=$result['cont_telefono'];
			$this->cont_celular=$result['cont_celular'];
			$this->cont_email=$result['cont_email'];
			
			if($result)
				return true;
			else
				return false;

		}else
			return NULL;
	}
	
	/***********************************************
	* funcion getArray
	* setea los datos en un arreglo
	***********************************************/
	function getArray(){
		if($this->cont_cod!=NULL){
			$array = array();
			$array['cont_cod']=$this->cont_cod;
			$array['usu_id']=$this->usu_id;
			$array['cont_nombre']=$this->cont_nombre;
			$array['cont_telefono']=$this->cont_telefono;
			$array['cont_celular']=$this->cont_celular;
			$array['cont_email']=$this->cont_email;

			return $array;
		}else
			return NULL;		
	}
	
	/***********************************************
	* funcion insertar
	* inserta un registro de la base
	***********************************************/
	function insertar(){
		$sql = "
			INSERT INTO `contactos` (
				`cont_cod`,
				`usu_id`,
				`cont_nombre`,
				`cont_telefono`,
				`cont_celular`,
				`cont_email`
			)VALUES (
				'".$this->cont_cod."',
				'".$this->usu_id."',
				'".$this->cont_nombre."',
				'".$this->cont_telefono."',
				'".$this->cont_celular."',
				'".$this->cont_email."'
				)";
		//die($sql);
		$result = $this->db->query($sql);
		if($result==true)
			return true;
		else
			return false;		
	}
	
	/***********************************************
	* funcion actualizar
	* actualiza un registro de la base en base al id
	***********************************************/
	function actualizar(){
		$sql = "
			UPDATE `contactos` SET
				`cont_nombre` = '".$this->cont_nombre."',
				`cont_telefono` = '".$this->cont_telefono."',
				`cont_celular` = '".$this->cont_celular."',
				`cont_email` = '".$this->cont_email."'
				 WHERE `cont_cod` = ".$this->cont_cod;	
		//die($sql);	
		$result = $this->db->query($sql);
		if($result==true)
			return true;
		else
			return false;	
	}
	
	/***********************************************
	* funcion actualizar
	* actualiza un registro de la base en base al id
	***********************************************/
	function eliminar(){
		$sql = "
			DELETE FROM contactos
			WHERE `cont_cod` = '".$this->cont_cod."'";
		//die($sql);
		$result = $this->db->query($sql);
		return $result;
	}
	
	/***********************************************
	* funcion getNotas
	* obtiene y devuevle una lista de los clienes
	***********************************************/
	function getContactos($letra){
		$lista=NULL;
		$sql = 	"SELECT c.`cont_cod`, c.`cont_nombre`, c.`cont_telefono`, c.`cont_celular`, c.`cont_email`
				FROM contactos c
				WHERE usu_id='".$this->usu_id."'
				AND cont_nombre like '".$letra."%'
				ORDER BY `cont_nombre`";	
		//die($sql." ");		
		$result = $this->db->query($sql);
		$arreglo=Array();
		if ($result === false) die("failed getContactos");
		$cont=0;
		for($i=0;$i<$result->num_rows;$i++)
			$arreglo[$i]=$result->fetch_assoc();
		return $arreglo;	
	}
	
	/////////////////////////////////////////////////
	/// Funcion para obtener el ultimo serial
	////////////////////////////////////////////////
	function obtenerUltimoContacto(){
		$sql = "SELECT MAX(cont_cod)
		        FROM contactos";
		$result = $this->db->query($sql);
		$result=$result->fetch_assoc();
		if($result)
			return $result["MAX(cont_cod)"];
		else
			return 0;
	}
	
	//Getters
	function getContCod(){
		return $this->cont_cod;
	}
	function getUsuarioId(){
		return $this->usu_id;
	}
	function getContNombre(){
		return $this->cont_nombre;
	}
	function getContTelefono(){
		return $this->cont_telefono;
	}
	function getContCelular(){
		return $this->cont_celular;
	}
	function getContEmail(){
		return $this->concont_email;
	}
	
	//Setters
	function setContCod($cont_cod){
		$this->cont_cod=$cont_cod;
	}
	function setUsuarioId($usu_id){
		$this->usu_id=$usu_id;
	}
	function setContNombre($cont_nombre){
		$this->cont_nombre=$cont_nombre;
	}
	function setContTelefono($cont_telefono){
		$this->cont_telefono=$cont_telefono;
	}
	function setContCelular($cont_celular){
		$this->cont_celular=$cont_celular;
	}
	function setContEmail($cont_email){
		$this->cont_email=$cont_email;
	}
}
?>